package com.wanghua.mall.product.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;

import com.wanghua.mall.product.entity.SkuInfoEntity;
import com.wanghua.mall.product.service.SkuInfoService;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;

import javax.annotation.Resource;


/**
 * sku信息
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-24 18:52:27
 */
@RestController
@RequestMapping("/product/skuinfo")
public class SkuInfoController {

    @Resource
    private SkuInfoService skuInfoService;

    /**
     * 根据skuID获取商品价格
     *
     * @param skuId
     * @return
     */
    @GetMapping("/getPriceBySkuId/{skuId}")
    public R getPriceBySkuId(@PathVariable("skuId") Long skuId) {
        BigDecimal price = skuInfoService.getById(skuId).getPrice();
        return R.ok().put("price", price.toString());
    }

    /**
     * 信息
     */
    @GetMapping("/skuInfo/{skuId}")
    public String skuInfo(@PathVariable("skuId") Long skuId) {
        return JSON.toJSONString(skuInfoService.getById(skuId));
    }

    /**
     * 条件列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = skuInfoService.queryPageCondition(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.save(skuInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.updateById(skuInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));
        return R.ok();
    }

}
